package com.cssnj.portal.web.query;

import com.cssnj.portal.domain.common.UserInfo;
import com.cssnj.portal.service.query.Query22Service;
import com.cssnj.portal.service.query.Query23Service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.predictech.framework.domain.common.JsonRequest;
import io.predictech.framework.domain.common.JsonResponse;
import io.predictech.framework.domain.exception.BaseException;
import io.predictech.framework.utility.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.List;

@Controller
@RequestMapping("/query/query23")
public class Query23Controller {

    @Autowired
    Query23Service query23Service;

    @RequestMapping({"/init.do"})
    public ModelAndView init(HttpServletRequest request) throws BaseException, SQLException ,JsonProcessingException {

        ModelAndView modelAndView = new ModelAndView("query/query23");
        UserInfo userInfo = (UserInfo)SecurityUtils.getSubject().getPrincipal();

        String cxjgDm = userInfo.getCxjgDm();
        if(cxjgDm.startsWith("1")){
            cxjgDm="23200000000";
        }

        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.setHandleCode("queryDataHy");
        jsonRequest.getReqData().put("swjgdm", cxjgDm);
        JsonResponse jsonResponse = query23Service.execute(jsonRequest);
        List dataList = (List)jsonResponse.getRepData().get("dataList");
        ObjectMapper objectMapper = StringUtil.getBaseObjectMapper();
        modelAndView.addObject("dataList1",objectMapper.writeValueAsString(dataList));

        jsonRequest.setHandleCode("queryDataDq");
        jsonRequest.getReqData().put("swjgdm", cxjgDm);
        jsonResponse = query23Service.execute(jsonRequest);
        dataList = (List)jsonResponse.getRepData().get("dataList");
        modelAndView.addObject("dataList2",objectMapper.writeValueAsString(dataList));

        return modelAndView;
    }
}
